<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";

require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "240px",
    },
    chartData: {
      type: Array,
      required: true,
    },
    chartColor: {
      type: Array,
      default: () => ["#3FC529", "#FBC225", "#E74629", "#E2E7EE"],
    },
    titleShow: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
      default: "0",
    },
  },
  data() {
    return {
      chart: null,
      total: 11,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.initChart();
      }, 100);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      this.chart.setOption({
        color: ["#3FC529", "#FBC225", "#E74629", "#E2E7EE"],
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          orient: 'vertical',
          padding: [60,30,0,0],
          left: "right",
          textStyle:{
            color:"#fff"
          }
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["40%", "60%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 20,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: chartData,
          },
        ],
      });
    },
  },
};
</script>
